<?php

namespace app\controllers;

use app\models\PayNotify;
use app\models\PayOrder;
use app\models\PointCharge;
use app\models\WxQr;
use Yii;
use app\components\Func;
use app\models\WxAccount;
use app\models\WxUser;
use app\models\WxMsg;
use yii\web\Controller;

class WeixinController extends Controller
{
    public $wxAccount;
    public $msg_arr;
    public function init(){
        parent::init();
        set_exception_handler([$this,'weixinException']);
        $this->wxAccount = WxAccount::findOne(1);
    }
    //微信扫码登录
    public function actionLogin(){
        $wxQr = new WxQr();
        $wxQr->wx_aid = 1;
        $wxQr->qr_type = WxQr::QR_TYPE_TEMP;
        $wxQr->scene_type = WxQr::SCENE_TYPE_STR;
        $wxQr->scene_value = Func::getcode(32);
        $wxQr->expire = 60;
        $wxQr->ctime = time();
        $wxQr->bindData('app\\models\\wxUser','scanQrLogin',[$wxQr->scene_value]);
        $wxQr->make();

//        $wxQr = WxQr::createTempObj(60,['app\\models\\wxUser','scanQrLogin',[]],1);
        return $this->render('login',['wxQr'=>$wxQr]);

    }
    //轮询检查登录状态
    public function actionCheckLogin(){
        $code = Yii::$app->request->post('code','');
        $open_id = Yii::$app->user_redis->get('wxlogincode:'.$code);
        $wxUser = WxUser::getObjByOpenid($open_id);
//        $wxUser = WxUser::findOne(1);
//        echo $open_id;
        if(empty($wxUser)) return;
//        print_r($wxUser);
        Yii::$app->user->login($wxUser);
        return 1;
    }
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
    public function actionMsg()
    {
        //启用微信服务器开发者模式检测
        $this->wxAccount->checkEchostr();
        //验证消息真实性
        if(!$this->wxAccount->checkMsgSign())
        {
            throw new \Exception('sign check error');
        }
        $result = file_get_contents('php://input');
        $this->msg_arr = Func::xmlToArray($result);
        $wxMsg = WxMsg::getTypeObj($this->wxAccount,$this->msg_arr);
        $wxUser = WxUser::getObjByOpenid($wxMsg->openid);
        if(!$wxUser){
            $wxUser = WxUser::addUser($wxMsg->wx_aid, $wxMsg->openid);
        }

        return $wxMsg->reply();

    }
    //支付通知
    public function actionPayNotify()
    {
        try{
            $result = file_get_contents('php://input');
            $arr = Func::xmlToArray($result);
            if(!isset($arr['return_code']) || $arr['return_code']!='SUCCESS'){
                throw new \Exception('参数错误1',2);
            }
            if(!isset($arr['result_code']) || $arr['result_code']!='SUCCESS'){
                throw new \Exception('参数错误11',2);
            }
            if(!isset($arr['out_trade_no']) || empty($arr['out_trade_no'])){
                throw new \Exception('参数错误2',2);
            }
            $payNotify = new PayNotify();
            $payNotify->pay_order_id = $arr['out_trade_no'];
            $payNotify->content = $result;
            $payNotify->save();
            $payOrder = PayOrder::findOne($arr['out_trade_no']);
            if(empty($payOrder)){
                throw new \Exception('参数错误3',2);
            }
            if(!$payOrder->payAccount->getPayObj()->checkNotifySign($arr)){
                throw new \Exception('签名错误',2);
            }
            if(!$payOrder->state==PayOrder::PAY_SUCCESS){
                throw new \Exception('OK',2);
            }
            $payOrder->state = PayOrder::PAY_SUCCESS;
            $payOrder->save();
            PointCharge::charge($payOrder->uid,PointCharge::TYPE_USER_CHARGE,$payOrder->total_fee,$payOrder->total_fee,$payOrder->id);
            return Func::arrayToXml([
                'return_code'=>'SUCCESS',
            ]);

        }catch (\Exception $e){
            return Func::arrayToXml([
                'return_code'=>'SUCCESS',
                'return_msg'=>$e->getMessage(),
            ]);
        }
    }
    //自定义异常捕获
    public function weixinException(\Throwable $e)
    {
        Yii::error($e->getMessage().$e->getFile().$e->getLine());
//        //异步客服消息
//        WxAccount::pushCustomMsg([
//            'raw_id'=> $this->msg_arr['ToUserName'],
//            'openid'=> $this->msg_arr['FromUserName'],
//            'msgtype' => 'text',
//            'text' => [
//                'content' => $e->getMessage(),
//            ],
//        ]);

        return 'success';
    }
}
